﻿Imports Persistencia
Imports Controle

Public Class relContatosARealizar
    Inherits relPadrao

#Region "Variáveis"
    Private objClientes As New perCliente
    Private objOcorrencias As New ctrOcorrencias
    Private iIdCliente As Int32
    Private iIdStatus As Int32
    Private iIdContato As Int32
    Private dtData As Date
    Private sCliente As String
    Private sStatus As String
    Private sContato As String
#End Region

#Region "Propriedades"

    Public Property NomeCliente() As String
        Get
            Return Me.sCliente
        End Get
        Set(ByVal value As String)
            Me.sCliente = value
        End Set
    End Property

    Public Property NomeStatus() As String
        Get
            Return Me.sStatus
        End Get
        Set(ByVal value As String)
            Me.sStatus = value
        End Set
    End Property

    Public Property NomeContato() As String
        Get
            Return Me.sContato
        End Get
        Set(ByVal value As String)
            Me.sContato = value
        End Set
    End Property

    Public Property IDCliente() As Int32
        Get
            Return Me.iIdCliente
        End Get
        Set(ByVal value As Int32)
            Me.iIdCliente = value
        End Set
    End Property

    Public Property IDStatus() As Int32
        Get
            Return Me.iIdStatus
        End Get
        Set(ByVal value As Int32)
            Me.iIdStatus = value
        End Set
    End Property

    Public Property IDContato() As Int32
        Get
            Return Me.iIdContato
        End Get
        Set(ByVal value As Int32)
            Me.iIdContato = value
        End Set
    End Property

    Public Property Data() As Date
        Get
            Return Me.dtData
        End Get
        Set(ByVal value As Date)
            Me.dtData = value
        End Set
    End Property

#End Region

#Region "Métodos privados"

    Private Sub relContatosARealizar_carregaDados(ByRef dsRPT As Object) Handles Me.carregaDados

        Dim dsContatosARealizar As New dsContatosARealizar
        Dim drContatosARealizar As dsContatosARealizar.ContatosARealizarRow
        Dim dtDados As New DataTable
        Dim drDados As DataRow
        Dim dtUltimoContao As Date
        Dim iIDContato As Int32
        Try
            dtDados = Me.objClientes.RetornarClientesPorDataContato(Me.Data, False, _
                                                                    Me.IDCliente, _
                                                                    Me.IDStatus, _
                                                                    Me.IDContato)

            If Not dtDados Is Nothing AndAlso dtDados.Rows.Count > 0 Then
                For Each drDados In dtDados.Rows
                    drContatosARealizar = DirectCast(dsRPT, dsContatosARealizar).ContatosARealizar.NewContatosARealizarRow
                    With drContatosARealizar
                        .IDCliente = perConversao.ToInt32(drDados.Item("IDCliente"))
                        .Cliente = perConversao.ToString(drDados.Item("RazaoSocial"))
                        .Status = perConversao.ToString(drDados.Item("Descricao"))
                        .Tolerância = perConversao.ToInt32(drDados.Item("Tolerancia"))
                        dtUltimoContao = objOcorrencias.RetornaUltimoContato(.IDCliente)
                        If Not dtUltimoContao = Date.MinValue Then
                            .UltimoContato = dtUltimoContao
                        End If
                        .ProximoContato = perConversao.ToDateTime(drDados.Item("ProximoContato"))
                        iIDContato = perConversao.ToInt32(drDados.Item("IDContato"))
                        If (iIDContato > 0) Then
                            .IDContato = perConversao.ToInt32(drDados.Item("IDContato"))
                            .Contato = perConversao.ToString(drDados.Item("NomeContato"))
                        End If

                        .Data = Me.Data
                    End With
                    DirectCast(dsRPT, dsContatosARealizar).ContatosARealizar.AddContatosARealizarRow(drContatosARealizar)
                Next
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'carregaDados'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Sub relClientes_carregaFormulas(ByRef crRPT As Object) Handles Me.carregaFormulas
        Dim sSubTitulo As String = ""

        If (Me.IDCliente <> 0) Then
            sSubTitulo &= "Cliente: " & Me.sCliente
        Else
            sSubTitulo &= "Cliente: todos"
        End If
        sSubTitulo &= "".PadLeft(3)

        If (Me.IDStatus <> 0) Then
            sSubTitulo &= "Status: " & Me.sStatus
        Else
            sSubTitulo &= "Status: todos"
        End If
        sSubTitulo &= "".PadLeft(3)

        If (Me.IDContato <> 0) Then
            sSubTitulo &= "Contato: " & Me.NomeContato
        Else
            sSubTitulo &= "Contato: todos"
        End If
        sSubTitulo &= "".PadLeft(3)

        With crRPT.DataDefinition.formulaFields
            .item("SubTitulo").text = "'" & sSubTitulo & "'"
        End With

    End Sub

#End Region

#Region "Métodos públicos"

    Public Function imprime(ByVal byDispositivoSaida As eDispositivoSaida) As Boolean
        Dim dsRPT As DataSet
        Dim crRPT As CrystalDecisions.CrystalReports.Engine.ReportClass
        Dim bProcesso As Boolean = False

        Try
            dsRPT = New dsContatosARealizar
            crRPT = New rptContatosARealizar

            crRPT.SummaryInfo.ReportTitle = "Contatos a realizar"

            bProcesso = objRelatorio.processa(byDispositivoSaida = eDispositivoSaida.impressora, dsRPT, crRPT)

            Return bProcesso
        Catch ex As Exception
            Throw New Exception("Exceção no método 'imprime'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region
End Class
